<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%@ include file="/head.jsp" %>

<body>

<div>
    <div class="layui-form layui-form-pane" style="margin: 5px;float: right">
        <input type="hidden" id="userId" value="${userId}"/>

        <div class="layui-form-item">
            <label class="layui-form-label">总金额：</label>
            <div class="layui-input-block">
                <input type="text" id="totalMoney"autocomplete="off" class="layui-input">
            </div>
        </div>

    </div>

    <%--    清除上面的浮动对下面布局的影响--%>
    <%--    <div style="clear: both;"></div>--%>

    <div class="layui-form layui-form-pane" style="clear: both;margin: 5px">
        <table id="demo" lay-filter="test"></table>
    </div>
</div>

<%--序号--%>
<script type="text/html" id="increase">
    {{d.LAY_TABLE_INDEX+1}}
</script>

<script type="text/html" id="createTime">
    {{# return layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss') }}
</script>
<script type="text/html" id="paymentTime">
    {{# return layui.util.toDateString(d.paymentTime, 'yyyy-MM-dd HH:mm:ss') }}
</script>

<script type="text/html" id="paymentType">
    {{# if (d.paymentType=== 1) { }}
    支付宝
    {{# } else if(d.paymentType=== 2) { }}
    微信
    {{# } else { }}

    {{# } }}
</script>

<script type="text/html" id="status">
    {{# if (d.status=== 0) { }}
    取消
    {{# } else if(d.status=== 1) { }}
    待付款
    {{# } else if(d.status=== 2) { }}
    已付款
    {{# } else { }}

    {{# } }}
</script>


<script type="text/html" id="videoImg">
    <!--使用viewer插件进行渲染-->
    <ul class="showImg">
        {{# layui.each(d.videoImg.split(','), function(index, item){ }}
        <li><img src="${url}{{item}}"/></li>
        {{# }); }}
    </ul>
</script>


<script type="text/html" id="barDemo">
    <c:if test="${model.glDelete != 1} && ${model.glUpdate !=1}">
        Without the permission
    </c:if>
    <a class="layui-btn layui-btn-xs" lay-event="goCheckVideo">审核视频</a>
    <c:if test="${model.glUpdate == 1}">
    </c:if>
    <c:if test="${model.glDelete == 1}">
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </c:if>
</script>


<script>
    layui.use(['util', 'laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'form'], function () {
        var table = layui.table,
            form = layui.form,
            $ = layui.jquery,//加载jquery对象
            laydate = layui.laydate,//加载jquery对象
            layer = layui.layer;

        laydate.render({
            elem: '#date' //指定元素
            , type: 'datetime'
            , range: true
            , trigger: 'click' //采用click弹出
        });

        var userId = $("#userId").val();

        //第一个实例
        table.render({
            elem: '#demo'
            , url: '/manage/webUser/findOrdersByPublishUserIdJson' //数据接口
            , where: {userId: userId}
            , page: true //开启分页
            , limit: 10 //初始页条数
            , cols: [[ //表头
                {field: 'id', title: 'ID', width: 80, templet: '#increase'}
                , {field: 'userNickName', title: '购买人', width: 150}
                , {field: 'videoName', title: '视频名称', width: 150}
                , {field: 'orderNo', title: '订单号', width: 200}
                , {field: 'paymentTime', title: '支付时间', width: 200, templet: '#paymentTime', sort: true}
                , {field: 'createTime', title: '订单创建时间', width: 200, templet: '#createTime', sort: true}
                , {field: 'paymentType', title: '支付类型', templet: '#paymentType'}
                , {field: 'status', title: '支付状态', templet: '#status', sort: true}
                , {field: 'payment', title: '付款金额', sort: true}
                // , {fixed: 'right', title: '操作栏', width: 280, align: 'left', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
            ]]
            , done: function (res) {
                $('#totalMoney').val(res.totalMoney);
                $(".showImg").viewer();
            }
            , id: 'testReload' //重载标识
        });

        var active = {
            // 方法级渲染的重载 不需要 #
            reload: function () {
                //获取条件
                var ordersStatus = $('#ordersStatus').val();
                var date = $('#date').val();

                //执行重载 testReload 对应第一个实例中 id
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        ordersStatus: ordersStatus,
                        date: date
                    }
                });
            }
            // 获取checkStatus行数据
            , dels: function () { //获取选中数据 demo 为table的id
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                var userIds = [];
                data.forEach(function (item, index) {
                    userIds.push(item.userId);
                });
                layer.confirm('真的删除吗?', function (index) {
                    $.ajax({
                        url: '/manage/videos/dels',
                        type: 'post',
                        dataType: 'json',
                        data: {userIds: userIds.join(",")},//join 将数组组装成 1,2,3 的形式
                        success: function (data) {
                            if (data.code == 200) {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            }
            , update: function () { //获取选中数据 demo 为table的id   data
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                if (data.length != 1) {
                    layer.msg("Select an action");
                    return false;
                }
                //data 获取的是数组,所以当修改的时候就要获取第一个数据
                var userId = data[0].userId;
                var index = layer.open({
                    title: '信息',
                    type: 2,
                    content: '/manage/videos/goEdit?userId=' + userId,
                    area: ['800px', '450px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.iframeAuto(index);
            }
        };

        $('.layui-form .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听工具条
        table.on('tool(test)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'goCheckVideo') { //审核视频
                window.location.href = '/manage/videosItem/findAll?videoId=' + data.videoId
            } else if (layEvent === 'del') { //删除
                layer.confirm('真的删除吗?', function (index) {
                    $.ajax({
                        url: '/manage/videos/del',
                        type: 'post',
                        dataType: 'json',
                        data: {videoId: data.videoId},
                        success: function (data) {
                            if (data.code == "200") {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            }
        });
    });
</script>

<%@ include file="/foot.jsp" %>
